<form class="layui-form" lay-filter="resultform" action="">
    <fieldset id="resadd" class="layui-elem-field width96p borderFFB800">
        <div class="layui-field-box">
            <div class="layui-form-item">
                <!-- <div class="layui-inline width50p">
                    <label class="layui-form-label width120 colorRed" for="Name">名称：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" name="OperatorNo" lay-verify="required" id="Name" placeholder="名称"
                            autocomplete="off">
                    </div>
                </div> -->
                <div class="layui-inline width50p">
                    <label class="layui-form-label width120 colorRed" for="SuperName">上级文件夹：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input readonly" name="SuperName" readonly="readonly" id="SuperName"
                            placeholder="" autocomplete="off">
                    </div>
                </div>
                <div class="layui-inline width50p">
                    <label class="layui-form-label width120 colorRed"></label>
                    <button type="button" class="layui-btn" id="uploadres">
                        <i class="layui-icon">&#xe67c;</i>上传附件
                    </button>
                </div>
            </div>
            <!-- <div class="layui-form-item">
                <div class="layui-inline width50p">
                    <label class="layui-form-label width120 colorRed" for="SuperName">上级文件夹：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input readonly" name="SuperName" readonly="readonly" id="SuperName"
                            placeholder="" autocomplete="off">
                    </div>
                </div>
            </div> -->
        </div>
    </fieldset>

    <div class="layui-form-item" style="margin-top:10px;">
        <div class="layui-input-block textaligncenter">
            <button class="layui-btn " id="ok">
                <i class="layui-icon">&#xe605; 确定</i>
            </button>
            <button class="layui-btn" type="button" id="cancel" lay-filter="cancel">
                <i class="layui-icon">&#xe65c; 取消</i>
            </button>
        </div>
    </div>

</form>

<script>
    layui.use(['form', 'upload', 'layer'], function () {
        let form = layui.form,
            upload = layui.upload,
            layer = layui.layer;
        //获取type的值 0 代表新增，

        let resid = "";
        let resname = "";
        let opttype = localStorage.getItem("OptType");
        let restype = localStorage.getItem("ResType");
        let supername = localStorage.getItem("SuperName");
        let superid = localStorage.getItem("SuperID");
        localStorage.setItem("OptType", null);
        localStorage.setItem("SuperName", null);
        localStorage.setItem("SuperID", null);
        localStorage.setItem("ResType", null);
        if (supername == "")
            $("#SuperName").val("根目录");
        else
            $("#SuperName").val(supername);

        let Token = "";

        function login() {
            var fromData = new FormData();
            fromData.append("userid", "admin");
            fromData.append("password", "000000");
            fromData.append("cookie", false);
            fromData.append("rememberLogin", false);
            $.ajax({
                type: "POST",
                url: layui.setter.url1 + '/api/_login/Login',
                async: false,
                contentType: false,
                processData: false,
                dataType: "json",
                mimeType: "multipart/form-data",
                data: fromData,
                success: function (res) {
                    if (res) {
                        Token = res.token_type + " " + res.access_token;
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.open({
                        tittle: "错误",
                        type: 0,
                        content: "调用接口异常"
                    });
                }
            });
        }

        login();

        let accepttype="";//layui上传文件类型
        let extstype="";//允许上传的文件后缀
        if(restype==0){//mp4
            accepttype="video";
            extstype="mp4|AVI|mov|rmvb|rm|FLV|3GP";
        }else if(restype==1){ //pdf
            accepttype="file";
            extstype="pdf";
        }else if(restype==3){ //rar
            accepttype="file";
            extstype="zip|rar|7z";
        }

        var uploadInst = upload.render({
            elem: '#uploadres',
            url: layui.setter.url1 + '/api/_file/Upload',
            headers: {
                "Authorization": Token
            },
            accept: accepttype,
            exts: extstype,
            before: function (obj) {
                layer.load(); //上传loading
            },
            done: function (data, index, upload) {
                layer.closeAll('loading');
                console.log(data);
                if (data) {
                    resid = data.Id;
                    resname = data.Name;
                } else {
                    layer.open({
                        tittle: "错误",
                        type: 0,
                        content: "上传错误"
                    });
                }
            },
            error: function (index, upload) {
                layer.closeAll('loading');
            },
            choose: function (obj) {

            }
        });

        //监听提交
        $("#ok").click(function (e) {
            $.ajax({
                type: "POST",
                url: layui.setter.url1 + '/api/Resource/AddResource',
                contentType: 'application/json',
                headers: {
                    "Authorization": Token
                },
                data: JSON.stringify({
                    "ParrentId": superid,
                    "Name": resname,
                    "FileId": resid,
                    "Type": restype,
                    "Content": ""
                }),
                success: function (res) {
                    let data = jQuery.parseJSON(res);
                    console.log(data);
                    if (data.Code == 200) {
                        layer.closeAll();
                        parent.location.reload();
                    } else {
                        layer.open({
                            tittle: "错误",
                            type: 0,
                            content: data.Msg
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.open({
                        tittle: "错误",
                        type: 0,
                        content: "调用接口异常"
                    });
                }
            });
            return false;
        });

        $("#cancel").click(function () {
            layer.closeAll();
            parent.location.reload();
            return false;
        });
    });
</script>